<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="枚举类型" prop="enumType">
        <el-select
          v-model="dataForm.enumType"
          filterable
          allow-create
          placeholder="请选择或输入枚举类型">
          <el-option
            v-for="type in enumTypes"
            :key="type"
            :label="type"
            :value="type">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="枚举键" prop="enumKey">
        <el-input v-model="dataForm.enumKey" placeholder="枚举键"></el-input>
      </el-form-item>
      <el-form-item label="枚举值" prop="enumValue">
        <el-input v-model="dataForm.enumValue" placeholder="枚举值"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input type="textarea" v-model="dataForm.description" placeholder="描述"></el-input>
      </el-form-item>
      <el-form-item label="排序号" prop="orderNum">
        <el-input-number v-model="dataForm.orderNum" :min="0" :max="1000"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      enumTypes: [],
      dataForm: {
        id: 0,
        enumType: '',
        enumKey: '',
        enumValue: '',
        description: '',
        orderNum: 0
      },
      dataRule: {
        enumType: [
          { required: true, message: '枚举类型不能为空', trigger: 'blur' }
        ],
        enumKey: [
          { required: true, message: '枚举键不能为空', trigger: 'blur' }
        ],
        enumValue: [
          { required: true, message: '枚举值不能为空', trigger: 'blur' }
        ],
        orderNum: [
          { required: true, message: '排序号不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.getEnumTypes()
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/doc/enum/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.enumType = data.enumConfig.enumType
              this.dataForm.enumKey = data.enumConfig.enumKey
              this.dataForm.enumValue = data.enumConfig.enumValue
              this.dataForm.description = data.enumConfig.description
              this.dataForm.orderNum = data.enumConfig.orderNum
            }
          })
        }
      })
    },
    // 获取枚举类型列表
    getEnumTypes () {
      this.$http({
        url: this.$http.adornUrl('/doc/enum/types'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.enumTypes = data.types
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/doc/enum/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'enumType': this.dataForm.enumType,
              'enumKey': this.dataForm.enumKey,
              'enumValue': this.dataForm.enumValue,
              'description': this.dataForm.description,
              'orderNum': this.dataForm.orderNum
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
